load("@aspect_bazel_lib//lib:tar.bzl", "tar")
load("@rules_distroless//apt:defs.bzl", "dpkg_status")
load("@rules_distroless//distroless:defs.bzl", "passwd")
load("@rules_oci//oci:defs.bzl", "oci_image")
load("//bazel:defs.bzl", "symlink_dir", "symlink_dir_test")
load(":system_tests.bzl", "oci_tar", "uvm_config_image")

package(default_visibility = ["//rs:system-tests-pkg"])

exports_files([
    "counter.wat",
])

# Packages we install into the image; see base image definition and its
# 'manifest = ...' field for more information
PACKAGES = [
    "@noble//bash/amd64",
    "@noble//ca-certificates/amd64",
    "@noble//coreutils/amd64",
    "@noble//libcryptsetup-dev/amd64",
    "@noble//dmsetup/amd64",
    "@noble//dosfstools/amd64",
    "@noble//e2fsprogs/amd64",
    "@noble//gawk/amd64",
    "@noble//gzip/amd64",
    "@noble//libssl3t64/amd64",
    "@noble//libunwind8/amd64",
    "@noble//mtools/amd64",
    "@noble//openssh-client/amd64",
    "@noble//rsync/amd64",
    "@noble//tar/amd64",
    "@noble//udev/amd64",
    "@noble//xz-utils/amd64",
    "@noble//zstd/amd64",
]

tar(
    name = "sh",
    mtree = [
        # needed as dpkg assumes sh is installed in a typical debian installation.
        "./usr/bin/sh type=link link=/bin/bash",
    ],
)

tar(
    name = "mkfsvfat",
    mtree = [
        # symlink instead of updating the PATH
        "./usr/bin/mkfs.vfat type=link link=/sbin/mkfs.vfat",
    ],
)

tar(
    name = "awk",
    mtree = [
        # we install the GNU variant but call 'awk' so we symlink
        "./usr/bin/awk type=link link=/usr/bin/gawk",
    ],
)

# Creates /var/lib/dpkg/status with installed package information.
dpkg_status(
    name = "dpkg_status",
    controls = [
        "%s:control" % package
        for package in PACKAGES
    ],
    target_compatible_with = [
        "@platforms//os:linux",
    ],
)

passwd(
    name = "passwd",
    entries = [
        dict(
            gecos = ["root"],
            gid = 0,
            home = "/root",
            shell = "/usr/bin/bash",
            uid = 0,
            username = "root",
        ),
    ],
)

oci_image(
    name = "ubuntu_test_runtime_image_src",
    architecture = "amd64",
    os = "linux",
    target_compatible_with = [
        "@platforms//os:linux",
    ],
    tars = [
        ":dpkg_status",
        ":passwd",  # needed because we ssh in

        # symlinks
        ":awk",
        ":mkfsvfat",
        ":sh",
    ] + PACKAGES,
)

oci_tar(
    name = "ubuntu_test_runtime.tar",
    image = ":ubuntu_test_runtime_image_src",
    repo_tags = ["ubuntu_test_runtime:image"],
)

uvm_config_image(
    name = "colocate_uvm_config_image",
    srcs = [
        ":ubuntu_test_runtime.tar",
        "//rs/tests:activate-systest-uvm-config",
    ],
    remap_paths = {
        "activate-systest-uvm-config": "activate",
    },
    tags = ["manual"],  # this target will be built if required as a dependency of another target
)

exports_files([
    "create-universal-vm-config-image.sh",
])

symlink_dir(
    name = "recovery/binaries",
    testonly = True,
    targets = {
        "//rs/registry/admin:ic-admin": "ic-admin",
        "//rs/state_tool:state-tool": "state-tool",
        "//rs/canister_sandbox:compiler_sandbox": "compiler_sandbox",
        "//rs/canister_sandbox:sandbox_launcher": "sandbox_launcher",
        "//rs/canister_sandbox:canister_sandbox": "canister_sandbox",
        "@crate_index//:ic-wasm__ic-wasm": "ic-wasm",
    },
)

symlink_dir_test(
    name = "cup_compatibility/binaries",
    targets = {
        "//rs/types/types:types_test": "types_test",
    },
)

oci_tar(
    name = "static-file-server.tar",
    image = "@static-file-server",
    repo_tags = ["static-file-server:image"],
)

exports_files([
    "jaeger_activate.sh",
])

uvm_config_image(
    name = "jaeger_uvm_config_image",
    srcs = [
        ":jaeger.tar",
        ":jaeger_activate.sh",
    ],
    remap_paths = {
        "jaeger_activate.sh": "activate",
    },
    tags = ["manual"],  # this target will be built if required as a dependency of another target
)

filegroup(
    name = "rosetta_workspace",
    srcs = glob(["rosetta_workspace/*"]),
)

exports_files([
    "src/canister_http/universal_vm_activation.sh",
])

oci_tar(
    name = "jaeger.tar",
    image = "@jaeger",
    repo_tags = ["jaegertracing/all-in-one:1.58"],
)

oci_tar(
    name = "minica.tar",
    image = "@minica",
    repo_tags = ["minica:image"],
)

oci_tar(
    name = "bitcoind.tar",
    image = "@bitcoind",
    repo_tags = ["bitcoind:image"],
)

oci_tar(
    name = "dogecoind.tar",
    image = "@dogecoind",
    repo_tags = ["dogecoind:image"],
)

oci_tar(
    name = "ic_gatewayd.tar",
    image = "@ic_gatewayd",
    repo_tags = ["ic_gatewayd:image"],
)

oci_tar(
    name = "nginx-proxy.tar",
    image = "@nginx-proxy",
    repo_tags = ["nginx-proxy:image"],
)

uvm_config_image(
    name = "ckbtc_uvm_config_image",
    srcs = [
        ":bitcoind.tar",
        ":dogecoind.tar",
        ":minica.tar",
        ":nginx-proxy.tar",
        "//ic-os/components:networking/dev-certs/canister_http_test_ca.cert",
        "//ic-os/components:networking/dev-certs/canister_http_test_ca.key",
        "//rs/tests/httpbin-rs:httpbin.tar",
    ],
    remap_paths = {
        "canister_http_test_ca.cert": "cert.pem",
        "canister_http_test_ca.key": "key.pem",
    },
    tags = ["manual"],  # this target will be built if required as a dependency of another target
)

uvm_config_image(
    name = "impersonate_upstreams_uvm_config_image",
    srcs = [
        ":minica.tar",
        ":static-file-server.tar",
        "//ic-os/components:networking/dev-certs/canister_http_test_ca.cert",
        "//ic-os/components:networking/dev-certs/canister_http_test_ca.key",
    ],
    remap_paths = {
        "canister_http_test_ca.cert": "minica.pem",
        "canister_http_test_ca.key": "minica-key.pem",
    },
    tags = ["manual"],  # this target will be built if required as a dependency of another target
)

uvm_config_image(
    name = "ic_gateway_uvm_config_image",
    srcs = [
        ":ic_gatewayd.tar",
    ],
    tags = ["manual"],  # this target will be built if required as a dependency of another target
)

oci_tar(
    name = "vector-with-log-fetcher.tar",
    image = "@vector-with-log-fetcher",
    repo_tags = [
        "vector-with-log-fetcher:image",
    ],
)

exports_files([
    "vector_activate.sh",
])

uvm_config_image(
    name = "vector_with_log_fetcher_image",
    srcs = [
        ":vector-with-log-fetcher.tar",
        ":vector_activate.sh",
    ],
    remap_paths = {
        "vector_activate.sh": "activate",
    },
    tags = ["manual"],
)
